package com.dq.service.service.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.vo.Vo;
import com.dq.domain.service.ServiceProject;
import com.dq.domain.service.ServiceProjectCategory;
import com.dq.domain.service.centre.ServiceProjectCategoryCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.service.ServiceProjectCategoryCentrService;
import com.dq.service.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cf
 */
@Service
public class ServiceProjectCategoryCentrServiceImpl extends BaseRepositories implements ServiceProjectCategoryCentrService {
    @Autowired
    private SysUserService sysUserService;


    @Override
    public Vo relation(List<Long> categoryIds, ServiceProject serviceProject) throws AdminException {
        SysUser user = sysUserService.getUser();

//        ServiceProject serviceProject = dao.get(ServiceProject.class, projectId);
        if (serviceProject == null) {
            throw new AdminException("无对应服务项目");
        }
        List<ServiceProjectCategoryCentre> list = serviceProjectCategoryCentreRepository.findByDeletedAndServiceProjectOrderByIdDesc(false, serviceProject);
        if (list != null && list.size() > 0) {
            serviceProjectCategoryCentreRepository.deleteAll(list);
        }
        if (categoryIds.isEmpty()) {
            return new Vo(HttpStatus.OK);
        }
        list = new ArrayList<>();
        List<ServiceProjectCategory> orgPositionList = serviceProjectCategoryRepository.findByIdIn(categoryIds);
        for (ServiceProjectCategory serviceProjectCategory : orgPositionList) {
            ServiceProjectCategoryCentre serviceProjectCategoryCentre = new ServiceProjectCategoryCentre();
            serviceProjectCategoryCentre.setServiceProjectCategory(serviceProjectCategory);
            serviceProjectCategoryCentre.setServiceProject(serviceProject);

            saveBaseOrgDeptPositionCentre(user, serviceProjectCategoryCentre);

            list.add(serviceProjectCategoryCentre);
        }
        serviceProjectCategoryCentreRepository.saveAll(list);

        return new Vo(HttpStatus.OK);
    }

    /**
     * 岗位服务关联 基本属性设置
     * @param user
     * @param serviceProjectCategoryCentre
     */
    public void saveBaseOrgDeptPositionCentre(SysUser user, ServiceProjectCategoryCentre serviceProjectCategoryCentre) {
        serviceProjectCategoryCentre.setVersion(Constant.VERSION_VALUE);
        serviceProjectCategoryCentre.setDeleted(false);
        serviceProjectCategoryCentre.setCreateTime(System.currentTimeMillis());
        serviceProjectCategoryCentre.setLastModifyTime(System.currentTimeMillis());
        serviceProjectCategoryCentre.setCreateUser(user);
        serviceProjectCategoryCentre.setUpdateUser(user);
    }

}
